package com.alibaba.citrus.cr.price.ability.functions.factory.price.process;

import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.platform.enhance.repository.CustomerRepository;
import com.epoch.app.crplatformenhance.dto.request.CustomerBatchRequest;
import com.epoch.app.crplatformenhance.sdo.CustomerSDO;
import com.epoch.app.crpricecenter.enums.DimensionPropertyEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author hanguo
 * @date 2021/11/19
 * @Description 客户
 */
@Component
public class CustomerProcess extends PriceProcess{
    /**
     * 客户查询服务
     */
    @Resource
    private CustomerRepository customerRepository;

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainCodeAndDescMap(List<String> codeList, boolean innerCode) {

        List<CustomerSDO> customerSDOList = obtainCustomerSDOList(codeList, innerCode);

        if (CollectionUtils.isEmpty(customerSDOList)) {
            return new HashMap<>();
        }

        if (innerCode) {
            return customerSDOList.stream().filter(item -> StringUtils.isNotBlank
                            (item.getCode()) && StringUtils.isNotBlank(item.getName())).
                    collect(Collectors.toMap(CustomerSDO::getCode, CustomerSDO::getName, (x, y) -> x));
        }
        return customerSDOList.stream().filter(item -> StringUtils.isNotBlank
                        (item.getCode()) && StringUtils.isNotBlank(item.getName())).
                collect(Collectors.toMap(CustomerSDO::getCode, CustomerSDO::getName, (x, y) -> x));

    }

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainInnerCodeAndOuterCodeMap(List<String> codeList, boolean innerCode) {

        List<CustomerSDO> customerSDOList = obtainCustomerSDOList(codeList, innerCode);

        if (CollectionUtils.isEmpty(customerSDOList)) {
            return new HashMap<>();
        }

        if (innerCode) {
            return customerSDOList.stream().filter(item -> StringUtils.isNotBlank
                            (item.getCode()) && StringUtils.isNotBlank(item.getOutCode())).
                    collect(Collectors.toMap(CustomerSDO::getCode, CustomerSDO::getOutCode,(x,y)->x));
        }
        return customerSDOList.stream().filter(item -> StringUtils.isNotBlank
                        (item.getOutCode()) && StringUtils.isNotBlank(item.getCode())).
                collect(Collectors.toMap(CustomerSDO::getOutCode, CustomerSDO::getCode,(x,y)->x));
    }

    @RepositoryInvoker
    public List<CustomerSDO> obtainCustomerSDOList(List<String> codeList, boolean innerCode) {

        List<CustomerSDO> customerSDOList = Lists.newArrayList();

        codeList = dealCodeList(codeList);
        if(Objects.isNull(codeList)){
            return customerSDOList;
        }
        CustomerBatchRequest customerBatchRequest = new CustomerBatchRequest();
        ListUtils.partition(codeList, 15).forEach(e -> {
            customerBatchRequest.setCodes(e);
            List<CustomerSDO> customerSubSDOList = customerRepository.batchQuery(customerBatchRequest);
            if (CollectionUtils.isNotEmpty(customerSubSDOList)) {
                customerSDOList.addAll(customerSubSDOList);
            }
        });
        return customerSDOList;
    }

    @Override
    public String getPropertyCode() {
        return DimensionPropertyEnum.CUSTOM_CODE.getKey();
    }
}
